<?php
namespace jianan\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use jianan\models\Cus;
use jianan\models\Master;
use yii\widgets\LinkPager;
use yii\helpers\Url;

header("Content-type:text/html;charset=utf-8");
/*
* 客户管理
*/

class CustomerController extends Controller {

	//客户列表
	public function actionIndex(){
		$model = new Cus;
		//搜索

		$query = Cus::find();		

		$countQuery = clone $query;

		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);
		if(!empty($_GET["Customer"]['name'])){
			$filter = $_GET["Customer"]['name'];
			if(!preg_match("/[^\d-,]/",$filter)){
				$phone = $filter;
				$model = $query->where(['LIKE','phone',$phone])->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=> SORT_DESC])->all();
			} else {
				$name = $filter;
				$model = $query->where(['like','name',$name])->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=> SORT_DESC])->all();
			}

		} else {
			$model = $query->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=> SORT_DESC])->all();
		}
		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		if(!empty($_GET['page'])&&!empty($_GET['per-page'])){
			return $this->render('index',['model'=>$model,'pages'=>$pages,'page'=>$_GET['page'],'perpage'=>$_GET['per-page'],'gender'=>$gender,'data'=>$data]);
		}else {

			return $this->render('index',['model'=>$model,'pages'=>$pages,'gender'=>$gender,'data'=>$data]);
		}
	}

	//添加客户
	public function actionAdd(){
		$model = new Cus;
		$data = Cus::$data;
	/*	var_dump($model->attributes);
		exit;*/
		if(!empty($_POST['Cus'])){

			$re = $_POST['Cus'];
			$re = [
				'name' => $re['name'],
				'phone' => $re['phone'],
				'type' => 0 ,
				'visitTime' => strtotime($re['visitTime']),
				'updateTime' => time(),
				'sid' => 0,
				'address' => $re['address'],
				'gender' => $re['gender'],
				'desc' => $re['desc'],
				'data' =>json_encode($_POST['data']),
				'age' => $re['age'],
				'sname' => '',
				'degree' => $re['degree'],
				'saleId' => '0',
				'IDNo' => $re['IDNo'],
				'tag' => $re['tag'],
				'country'=> '',
				'zipcode' => $re['zipcode'],
				'email' => $re['email']
			];
		
			$model->attributes = $re;


			if($model->save(false)){				
				Yii::$app->session->setFlash('info','添加成功');
			} else {
				Yii::$app->session->setFlash('info','添加失败');
			}
			return $this->redirect(['customer/add']);
		}

		return $this->render('add',['model'=>$model,'data'=>$data]);
	}

	public function actionEdit(){
		$id = $_GET['id'];
		$model = Cus::find()->where(['id'=>$id])->asArray()->one();
		// var_dump($model);
		// exit;
		$data = Cus::$data;
		//数据表的data
		$result = json_decode($model['data']);

		$res = (array)$result;
		// var_dump($res);
		// exit;
		$model['price'] = $res['price'];
		$model['totalprice'] = $res['totalprice'];
		$model['floor'] = $res['floor'];

		//遍历 数据
		foreach($res as $key=>$val){
			foreach($data as $k =>$v){
				if($key==$k){
					$re[$key] = $v[$val];					
				}
			}
		}
		
		$model['jobarea'] = $re['jobarea'];
		$model['huxing'] = $re['huxing'];
		$model['area'] = $re['area'];		
		$model['konwWay'] = $re['konwWay'];
		$model['purpose'] = $re['purpose'];
		$model['job'] = $re['job'];


		return $this->render('edit',['model'=>$model,'result'=>$res]);
	}

	public function actionApproval(){
		// var_dump($_GET['id']);
		// var_dump($_GET['approval']);
		// var_dump($_GET['page']);
		// var_dump($_GET['perpage']);
		// exit;
		if(isset($_GET['id']) && isset($_GET['approval']) && isset($_GET['page'])&& isset($_GET['perpage'])){


			$model = Cus::findOne($_GET['id']);
			$model->approval=1;
			if($model->save(false)){				
				Yii::$app->session->setFlash('info','审批提交成功');
			} else {
				Yii::$app->session->setFlash('info','审批提交失败失败');
			}
			return $this->redirect(['customer/index','page'=>$_GET['page'],'per-page'=>$_GET['perpage']]);
		} else {

			$model = Cus::findOne($_GET['id']);
			$model->approval=1;
			if($model->save(false)){				
				Yii::$app->session->setFlash('info','审批提交成功');
			} else {
				Yii::$app->session->setFlash('info','审批提交失败失败');
			}
			return $this->redirect(['customer/index']);

		}

	}
}
